package cxydmmszl.chapter08.t116;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * <li style="color: red;">Prob</li>
 * 将正方形矩阵顺时针旋转 90 度
 * <li style="color: green;">Desc</li>
 * 给定一个 n*n 的矩阵 matrix，请把这个矩阵顺时针转动 90 度。
 * <li style="color: green;">Input</li>
 * 输入包含多行，第一行两个整数 n(1≤n≤200)，代表矩阵的行数和列数，
 * 接下来 n 行，每行 n 个整数，代表矩阵 matrix(1≤matrix[i][j]≤40000)。
 * <li style="color: green;">Output</li>
 * 输出旋转后的矩阵（包含n行，每行n个数）。
 * <li style="color: blue;">Link</li> CD150
 *
 * @author habitplus
 * @since 2021-10-07 16:57
 */
public class Main {
    static StreamTokenizer st =
            new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int nextInt() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) st.nval;
    }

    public static void main(String[] args) {
        int n = nextInt();
        int[][] matrix = new int[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = nextInt();
            }
        }

        rotateMatrix(matrix);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                sb.append(matrix[i][j]).append(" ");
            }
            sb.append("\n");
        }
        System.out.println(sb.toString());
    }

    private static void rotateMatrix(int[][] matrix) {
        if (matrix == null
                || matrix.length == 0
                || matrix[0].length == 0
                || matrix.length != matrix[0].length) {
            return;
        }

        int lt = 0;
        int rb = matrix.length - 1;

        int times, tmp;
        while (lt < rb) {
            times = rb - lt;
            for (int i = 0; i < times; i++) {
                tmp = matrix[lt][lt + i];
                matrix[lt][lt + i] = matrix[rb - i][lt];
                matrix[rb - i][lt] = matrix[rb][rb - i];
                matrix[rb][rb - i] = matrix[lt + i][rb];
                matrix[lt + i][rb] = tmp;
            }
            lt++;
            rb--;
        }
    }
}
